package com.sy.config;

import com.sy.interceptor.JwtTokenAdminInterceptor;
import com.sy.interceptor.JwtTokenUserInterceptor;
import com.sy.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    //用户端拦截器
    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;
    /**
     * 注册自定义拦截器
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        //注册前台用户端拦截器
        //注册用户端拦截器
        registry.addInterceptor(jwtTokenUserInterceptor)
                //需要拦截的请求
                .addPathPatterns("/user/course/auth/**")
                .addPathPatterns("/user/pay/**")
                .addPathPatterns("/user/orders/**")
                .addPathPatterns("/user/user/auth/**");
        //设置不需要拦截的请求
        //.excludePathPatterns("/user/user/login");

        //注册后台员工端拦截器
        registry.addInterceptor(jwtTokenAdminInterceptor)
                //拦截的请求
                .addPathPatterns("/admin/**")
                //放行的请求
                .excludePathPatterns("/admin/employee/login");
    }

    /**
     * 通过knife4j生成接口文档
     * @return
     */
    @Bean
    public Docket docket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("在线教育项目接口文档")
                .version("2.0")
                .description("胜雅教育在线教育项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                //扫描com.sy.controller包，为当前包生成接口
                .apis(RequestHandlerSelectors.basePackage("com.sy.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 设置静态资源映射
     * 把/doc.html映射到classpath:/META-INF/resources/中，也就是访问这个文件会从
     * classpath:/META-INF/resources/匹配
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

    /**
     * 追加自定义转换器
     * @param converters
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

        //获取json数据转换器封装类
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

        //设置一个自定义的转换器
        converter.setObjectMapper(new JacksonObjectMapper());

        //追加到springmvc集合中，并且设置为下标为0，这样自定义转换器优先级高
        converters.add(0,converter);
    }
}
